﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class ManageAssignment : System.Web.UI.Page
{
    DataTable dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            LoadSubject();
        }
    }

    private void LoadSubject()
    {
        dt = new DataTable();
        dt = AdminCreateTestDAL.GetSubjects();
        ddlSub.Items.Add("--Select One--");
        ddlSub.SelectedIndex = ddlSub.Items.IndexOf(ddlSub.Items.FindByText("--Select One--"));
        ListItem list = new ListItem();
        list.Text = "All";
        list.Value = "0";
        ddlSub.Items.Add(list);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem li = new ListItem();
            li.Text = dt.Rows[i][0].ToString();
            li.Value = dt.Rows[i][1].ToString();
            ddlSub.Items.Add(li);
        }
    }

    private void bindGrid()
    {
        dt = new DataTable();
        dt = AdminCreateTestDAL.GetAssignmentAllInfo(Convert.ToInt32(ddlSub.SelectedValue), Convert.ToInt32(ddlTopic.SelectedValue), ddlStatus.SelectedValue);
        gvAssignment.DataSource = dt;
        gvAssignment.DataBind();
    }

    protected void ddlSub_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlTopic.Items.Clear();
        if (ddlSub.SelectedItem.Text == "--Select One--")
        {
            lablError.Text = "Please select one subject";

        }
        else
        {
            dt = new DataTable();
            ListItem list = new ListItem();
            list.Text = "All";
            list.Value = "0";
            ddlTopic.Items.Add(list);
            dt = AdminCreateTestDAL.getTopicRelatedToSubject(Convert.ToInt32(ddlSub.SelectedItem.Value));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListItem li = new ListItem();
                li.Text = dt.Rows[i][0].ToString();
                li.Value = dt.Rows[i][1].ToString();
                ddlTopic.Items.Add(li);
            }
        }
    }

    protected void ClearRecord_Click(object sender, EventArgs e)
    {
        gvAssignment.DataSource = null;
        gvAssignment.DataBind();
    }

    protected void btnShow_Click(object sender, EventArgs e)
    {
        bindGrid();
    }
    protected void gvAssignment_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvAssignment.PageIndex = e.NewPageIndex;
        gvAssignment.SelectedIndex = -1;
        bindGrid();
    }

    protected void gvAssignment_SelectedIndexChanged(object sender, EventArgs e)
    {
        int assId = Convert.ToInt32(gvAssignment.SelectedDataKey[0]);
        string status = gvAssignment.SelectedDataKey[1].ToString();
        status = status.Trim();
        if (status == "Scheduled")
            Response.Redirect("~/Admin/CreateAssignment.aspx?AssignmentId=" + assId.ToString());

    }

    protected void gvAssignment_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int assId = Convert.ToInt32(e.CommandArgument);
        switch (e.CommandName)
        {
            case "btnCopyClick": Response.Redirect("~/Admin/CreateAssignment.aspx?CopyAssignmentId=" + assId.ToString());
                break;

            case "btnLinkClick": Response.Redirect("~/Admin/ViewAssignmentReport.aspx?AssignmentId=" + assId.ToString());
                break;

            case "btnLinkRankClick": Response.Redirect("../Student/ViewAssignmentRank.aspx?AssignmentId=" + assId.ToString());
                break;
        }  
    }
}